package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.*;
import com.zzyl.nursing.vo.FamilyMemberElderListVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService {
    @Autowired
    private IElderService elderService;
    @Autowired
    private IBedService bedService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IDeviceService deviceService;
    //查找我的老人分页
    @Override
    public List<FamilyMemberElderListVo> listByPage(Integer pageNum, Integer pageSize) {
        Page<FamilyMemberElder> page = new Page<>(pageNum,pageSize);
        Long userId = UserThreadLocal.get();
        LambdaQueryWrapper<FamilyMemberElder> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(FamilyMemberElder::getFamilyMemberId,userId);
        page=page(page,queryWrapper);
        List<FamilyMemberElderListVo> records =new ArrayList<>();
        for (FamilyMemberElder record : page.getRecords()){
            FamilyMemberElderListVo familyMemberElderListVo = new FamilyMemberElderListVo();
            familyMemberElderListVo.setMid(record.getFamilyMemberId().toString());
            familyMemberElderListVo.setMremark(record.getRemark());
            familyMemberElderListVo.setElderId(record.getElderId().toString());
            //获取老人信息
            Elder elder = elderService.selectElderById(record.getElderId());
            familyMemberElderListVo.setName(elder.getName());
            familyMemberElderListVo.setImage(elder.getImage());
            familyMemberElderListVo.setBedNumber(elder.getBedNumber());
            //获取房间类型
            LambdaQueryWrapper<Bed> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Bed::getBedNumber,elder.getBedNumber());
                    //床位信息
            Bed bed = bedService.getOne(queryWrapper2);
                    //房间类型
            LambdaQueryWrapper<Room> queryWrapper3 =new LambdaQueryWrapper<>();
            queryWrapper3.eq(Room::getId,bed.getRoomId());
            Room room = roomService.getOne(queryWrapper3);
            familyMemberElderListVo.setTypeName(room.getTypeName());

            //获取设备信息
            LambdaQueryWrapper<Device> queryWrapper4 =new LambdaQueryWrapper<>();
            queryWrapper4.eq(Device::getBindingLocation,record.getElderId())
                    .eq(Device::getLocationType,"0")
                    .eq(Device::getPhysicalLocationType,"-1");
            Device device = deviceService.getOne(queryWrapper4);
            if (ObjectUtil.isNotEmpty(device)){
                familyMemberElderListVo.setIotId(device.getIotId());
                familyMemberElderListVo.setDeviceName(device.getDeviceName());
                familyMemberElderListVo.setProductKey(device.getProductKey());
            }
            records.add(familyMemberElderListVo);
        }
        //Page<FamilyMemberElderListVo> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        //resultPage.setRecords(records);

        return records;
    }

    //查找我的老人
    @Override
    public List<FamilyMemberElderVo> my() {
        Long userId = UserThreadLocal.get();
        LambdaQueryWrapper<FamilyMemberElder> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FamilyMemberElder::getFamilyMemberId,userId);
        List<FamilyMemberElderVo> list =new ArrayList<>();
        list(queryWrapper).forEach(item->{
            FamilyMemberElderVo elderVo = BeanUtil.copyProperties(item, FamilyMemberElderVo.class);
            elderVo.setElderName(elderService.selectElderById(item.getElderId()).getName());
            list.add(elderVo);
        });
        return list;
    }


}
